--------------------------------------------------------------------------------
--- GetInstanceName:
--- it returns the name of an instance if it have already been parsed
--------------------------------------------------------------------------------
function GetInstanceName obj instCont =
(
	for instances in instCont do
	(
		--format "\tChecking instances %\n" instances
		for i in instances do
		(
			if (i == obj) then
			(
				format "\t Instance found!\n"

				return instances[1].name
			)
		)
	)
	
	return undefined
)

--------------------------------------------------------------------------------
--- parseInstances:
--- it parses the level geometry and checks the instances to add them only once
--- as a meshes on the xml file
--------------------------------------------------------------------------------
function parseInstances root file instanceContainer =
(
	ch = root.children
	
	format "\t<geometry>\n"  to:file
	
	for c in ch do
	(
		if (findString c.name "SubNode" != undefined) then
		(
			objects = c.children
			for o in objects  do
			(
				format "parsing instance with name: %\n" o.name
				local name = GetInstanceName o instanceContainer
				
				if name == undefined then
				(
					InstanceMgr.GetInstances o &instances
					instanceContainer [instanceContainer.count + 1] = instances
					name = instances[1].name
				)
				
				format "Instance name to be used: %\n" name

				
				local filepath = "E:\\Atha-nor\\Tools\\test\\" + name + ".fbx"
				
				format "\t\t<mesh name=\"%\" path=\"%\"/>\n" name filepath to:file
			)
		)
		else
		(
		)
	)
	
	format "\t</geometry>\n"  to:file
)

--------------------------------------------------------------------------------
--- parseObject:
--- This parses an object holding from a node. It writes to the xml file its 
--- position, rotation, etc
--------------------------------------------------------------------------------
function parseObject obj file instanceContainer  =
(
	format "Inspecting object %...\n" obj.name
		
	name = GetInstanceName obj instanceContainer
	if name == undefined then
	(
		format "\t No instance found!\n" obj.name
		
		InstanceMgr.GetInstances obj &instances
		instanceContainer [instanceContainer.count + 1] = instances
		select(obj)
		exportFile ("E:\\Atha-nor\\Tools\\test\\" + obj.name + ".fbx") #noPrompt
		name = instances[1].name
	)
		
	format "\t Object name used %\n" name
		
	euler = quatToEuler obj.rotation
	format "\t\t\t\t<object name=\"%\" position=\"% % %\" rotation=\"% % %\"/>\n" name obj.position.x obj.position.y obj.position.z euler.x euler.y euler.z to:file
)

--------------------------------------------------------------------------------
--- exportLevel:
--- This is the main function that exports the entire level to an xml file 
--- and to fbx files
--------------------------------------------------------------------------------
function exportLevel selected =
(
	if (selected == undefined) then
	(
		format "You must select a Node\n"
	)
	else
	(
		if (selected.parent == undefined and selected.name == "Root") then
		(
			local instanceContainer = #()
			root = selected
			levelname = getFilenameFile(MaxFileName)
			
			format "Creating xml file\n"
			file = openFile ("E:\\Atha-nor\\Tools\\test\\" + levelname  + ".xml") mode:"wt"
			
			format "<%>\n" levelname  to:file
			
			format "Parsing instances...\n"
			parseInstances root file instanceContainer

			format "\t<Root>\n" to:file
			
			format "Parsing objects...\n"
			-- We selected the root node so we want to export the entire level
			ch = selected.children
			for c in ch do
			(
				if (findString c.name "SubNode" != undefined) then
				(
					-- Geometry hangs from subnodes that hangs from root
					format ("\t\t<" + c.name + ">\n") to:file

					-- Parse subnode
					objects = c.children
					for o in objects do
					(
						parseObject o file instanceContainer
					)
					
					format ("\t\t</" + c.name + " >\n") to:file
				)
				else
				(
					-- Geometry directly hangs from root
					parseObject c file
				)
			)
			
			select(root)
			format "\t</Root>\n" to: file
			format "</%>\n" levelname  to:file

			close file
		)
		--else if (findString selection[1].name "SubNode" != undefined) then
		--(
		--)
		else
		(
			format "You should select a Root Node to make this work\n"
		)
	)
)



------------------------------------------------------------------
------------------------------------------------------------------
---------------- START PROGRAM -----------------------------------
------------------------------------------------------------------
------------------------------------------------------------------
clearListener()
exportLevel selection[1]
